//
//  AY_DirectoryPage.m
//  AiYue
//
//  Created by boWen on 2023/11/20.
//

#import "AY_DirectoryPage.h"
#import "AY_DirectoryCell.h"
@interface AY_DirectoryPage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic ,strong) NSArray *chapters;
@property (nonatomic, assign) CGFloat tableHeight;
@property (nonatomic, strong) UILabel * chapterLabel;
@property (nonatomic, strong) UILabel * sortLabel;
@end

@implementation AY_DirectoryPage

- (instancetype)initDirectoryPageDataSource:(NSArray *)dataSource {
    if (self = [super init]) {
        self.chapters = dataSource;
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    [self initPop];
    [self.view addSubview:self.baseTableView];
    [self setupTopView];
    [self setupDownView];
}

- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat height = YNUM(20);
    height += 12 * YNUM(44);
    self.tableHeight = height;
    self.contentSizeInPopup = CGSizeMake(self.view.frame.size.width, height);
    self.popupController.navigationBarHidden = YES;
    self.gk_navigationBar.hidden = YES;
    self.popupController.containerView.layer.masksToBounds = YES;
    self.popupController.containerView.layer.cornerRadius = YNUM(13);
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeBackgroundTap)]];
}

- (void)closeBackgroundTap  {
    [self.popupController dismiss];
}

- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, YNUM(45), GK_SCREEN_WIDTH, self.tableHeight - YNUM(40) - YNUM(45))];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor whiteColor];
        [myTableView registerClass:[AY_DirectoryCell class] forCellReuseIdentifier:@"AY_DirectoryCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_DirectoryCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_DirectoryCell"];
    if (self.chapters.count > indexPath.section) {
        cell.chapterModel = self.chapters[indexPath.section];
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.chapters.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ReadChapterModel *model = self.chapters[indexPath.section];
    if ([self.delegate respondsToSelector:@selector(didSelectMuluWithChapterIndex:)]) {
        [self.delegate didSelectMuluWithChapterIndex:model.sortNo.integerValue - 1];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(44);
}


- (void)setupDownView {
    UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, self.tableHeight - YNUM(40), SCREEN_W, YNUM(40))];
    footerV.backgroundColor = [UIColor whiteColor];
    UIButton * sureBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"关闭" andFont:[UIFont boldSystemFontOfSize:16] andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(handleCloseAction) andRadius:YNUM(8) andAddSubView:footerV];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(footerV);
        make.left.equalTo(footerV).offset(YNUM(16));
        make.right.equalTo(footerV).offset(YNUM(-16));
        make.height.mas_offset(YNUM(44));
    }];
    [self.view addSubview:footerV];
}

- (void)setupTopView {
    UIView * bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, YNUM(45))];

    self.chapterLabel = [AYTool createLabelWithFrame:CGRectZero andText:[NSString stringWithFormat:@"共%ld章", self.chapters.count] andTextColor:COLOR_BLACK andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:1 andRadius:0 andAddSubView:bg];
    [self.chapterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bg);
        make.left.equalTo(bg).offset(YNUM(12));
        make.height.mas_offset(YNUM(22));
    }];
    
    
    self.sortLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"正序" andTextColor:COLOR(@"#FD8301") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:bg];
    [self.sortLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(22));
        make.right.equalTo(bg).offset(YNUM(-16));
        make.centerY.equalTo(bg);
    }];
    
    UIImageView * sortImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_sort.png") andBg:[UIColor clearColor] andRadius:0.0 andAddSubView:bg];
    sortImgView.userInteractionEnabled = YES;
    [sortImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(20));
        make.width.mas_equalTo(YNUM(20));
        make.right.equalTo(self.sortLabel.mas_left).offset(YNUM(-2));
        make.centerY.equalTo(bg);
    }];
    
    UIButton * sortBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:[UIColor clearColor] andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(sortWithAction:) andRadius:YNUM(8) andAddSubView:bg];
    [sortBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bg);
        make.right.equalTo(bg);
        make.height.mas_offset(YNUM(44));
        make.width.mas_offset(YNUM(45));
    }];
    
    [self.view addSubview:bg];
}

- (void)handleCloseAction {
    [self closeBackgroundTap];
}


#pragma mark - 排序
- (void)sortWithAction:(UIButton *)btn {
    if (!self.chapters || [self.chapters count] <= 0) {
        return;
    }
    btn.selected = !btn.selected;
    if (btn.selected) {
        self.sortLabel.text = @"倒序";
        self.chapters = [[self.chapters reverseObjectEnumerator] allObjects];
        [self.baseTableView reloadData];
        [self.baseTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }else {
        self.sortLabel.text = @"正序";
        self.chapters = [[self.chapters reverseObjectEnumerator] allObjects];
        [self.baseTableView reloadData];
        [self.baseTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
}

@end
